import 'dart:collection';
import 'dart:convert';
import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/merchant/GoodsGuige.dart';
import 'package:shop/shop/merchant/Index.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
// import 'package:awesome_dropdown/awesome_dropdown.dart';

class GoodsInfoEdit extends StatefulWidget {
  List guiges = [];//商品规格集合
  var tu1;var tu2;var tu3;var tu4;var tu5;//商品图片
  var tu6;var tu7;var tu8;//商品详情图片
  String gName;String gPrice;String danwei;String kucun;String miaoshu;String goodsTypeName;//商品信息
  int type;//类型  1：新增  2：修改
  String id;//商品ID
  String fuwuPicture;   //服务图片json
  String detailPicture; //详情图片json
  GoodsInfoEdit({Key key, this.title, this.guiges,this.kucun,this.gPrice,this.goodsTypeName,this.miaoshu,this.gName,this.danwei,this.tu1,this.tu2,this.tu3,this.tu4,this.tu5,this.tu6,this.tu7,this.tu8,this.type,this.id,this.fuwuPicture,this.detailPicture}) : super(key: key);

  final String title;

  @override
  _GoodsInfoEdit createState() => _GoodsInfoEdit(guiges,kucun,gPrice,goodsTypeName,miaoshu,gName,danwei,tu1,tu2,tu3,tu4,tu5,tu6,tu7,tu8,type,id,fuwuPicture,detailPicture);
}

class _GoodsInfoEdit extends State<GoodsInfoEdit> {

  TextEditingController _gName;
  TextEditingController _gPrice;
  TextEditingController _danwei;
  TextEditingController _kucun;
  TextEditingController _miaoshu;

  String fuwuPictures;  //服务图片json
  String detailPictures;//详情图片json
  List fuwuPicture = [];//服务图片集合
  List detailPicture = [];//详情图片集合

  var isMoreGuige=false;//是否支持多规格
  List<DropdownMenuItem> goodsTypeList = []; //商品类型集合
  var goodsTypeName;    //商品类型名称
  var merchantType;     //店铺类型
  String merchantID;    //商户ID
  dynamic _imgPath1;    //第一个图片的显示地址
  dynamic _imgPath2;dynamic _imgPath3;dynamic _imgPath4;dynamic _imgPath5;dynamic _imgPath6;dynamic _imgPath7;dynamic _imgPath8;
  var tu1;              //服务器返回的第一个图片的地址
  var tu2;var tu3;var tu4;var tu5;var tu6;var tu7;var tu8;
  int type=1;//类型  1：新增  2：修改
  String id;//商品ID
  SharedPreferences prefs;
  String mType="0";//商户类型

  List guiges = [];//商品规格集合
  String gName="";String gPrice="";String danwei="";String kucun="";String miaoshu="";//商品信息
  _GoodsInfoEdit(guiges,kucun,gPrice,goodsTypeName,miaoshu,gName,danwei,tu1,tu2,tu3,tu4,tu5,tu6,tu7,tu8,type,id,fuwuPicture,detailPicture){
    this.fuwuPictures=fuwuPicture;
    this.detailPictures=detailPicture;
    this.guiges=guiges;this.tu1=tu1;this.tu2=tu2;this.tu3=tu3;this.tu4=tu4;this.tu5=tu5;
    this.tu6=tu6;this.tu7=tu7;this.tu8=tu8;
    this.gName=gName;this.kucun=kucun;this.gPrice=gPrice;this.miaoshu=miaoshu;this.danwei=danwei;
    this.type=type;this.id=id;
    if(goodsTypeName!=null && goodsTypeName!=""){
      this.goodsTypeName=goodsTypeName;
    }
    if(guiges.length>0){
      isMoreGuige = true;
    }
    _gName = new TextEditingController(text: gName);
    _gPrice = new TextEditingController(text: gPrice);
    _danwei = new TextEditingController(text: danwei);
    _kucun = new TextEditingController(text: kucun);
    _miaoshu = new TextEditingController(text: miaoshu);
    // if(type==2){
      if(fuwuPictures !=null && fuwuPictures != ""){
        this.fuwuPicture = json.decode(fuwuPictures);
      }
      if(detailPictures !=null && detailPictures != ""){
        this.detailPicture = json.decode(detailPictures);
      }
    // }
    _getSundryInfo();
  }

  //得初始数据
  _getSundryInfo() async {
    // 获取内存存储的用户id
    prefs = await SharedPreferences.getInstance();
    if(prefs.getBool("isLogin")){
    // if(true){
      merchantID = prefs.getString("merchantID");
      mType = prefs.getString("mType");
      //得商家所有商品类型
      var x = await HttpUtil().get("/waimai/mGoodsType/getMerchantAllGoodsType?mId="+merchantID);
      if(x['code']==200){
        if(x['data'].length>0){
          if(goodsTypeName==null || goodsTypeName==""){
            goodsTypeName = x['data'][0]['name'];
          }
          for(int i=0;i<x['data'].length;i++){
            goodsTypeList.add(DropdownMenuItem(child: Text(x['data'][i]['name']),value: x['data'][i]['name'],),);
          }
        }
      }
      if(type==2){
        fuwuPicture = json.decode(fuwuPictures);
        detailPicture = json.decode(detailPictures);
      }
      setState(() {});
    }else{
      // Fluttertoast.showToast(msg: "请先登录");
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        automaticallyImplyLeading: false,
        title: Text("商品编辑"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(10, 5, 0, 5),
              decoration: BoxDecoration(
                color: Colors.black12,
              ),
              child: Row(
                children: [
                  Icon(Icons.warning_outlined,size: 20,color: Colors.red,),
                  Text("标"),Text("*",style: TextStyle(color: Colors.red),),
                  Text("为必填项，其余的为选填项")
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("商品名称"),SizedBox(width: 5,),],) ,
                  SizedBox(width: 30,),
                  Expanded(child: TextFormField(
                    controller: _gName,
                    decoration: InputDecoration(
                      hintText: "请输入商品名称",
                      hintStyle: TextStyle(fontSize: 14)
                      // border: InputBorder.none,
                    ),
                  ))
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("商品类型"),SizedBox(width: 5,),],) ,
                  SizedBox(width: 30,),
                  Expanded(
                    child: DropdownButton(
                      hint: Text("请选择商品类型"),
                      value: goodsTypeName,
                      items: goodsTypeList,
                      isExpanded: true,
                      onChanged: (value) {
                        setState(() {
                          goodsTypeName=value;
                        });
                      }
                    )
                  )
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("商品价格"),SizedBox(width: 5,),],) ,
                  SizedBox(width: 30,),
                  Expanded(child:TextFormField(
                    controller: _gPrice,
                    decoration: InputDecoration(
                      hintText: "请输入商品价格",
                      hintStyle: TextStyle(fontSize: 14)
                    ),
                  ))
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red)),Text("商品单位"),SizedBox(width: 5,),],) ,
                  SizedBox(width: 30,),
                  Expanded(child:TextFormField(
                    controller: _danwei,
                    decoration: InputDecoration(
                      hintText: "个/份/只/箱/....",
                      hintStyle: TextStyle(fontSize: 14)
                    ),
                  ))
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red),),Text("商品库存"),SizedBox(width: 5,)],) ,
                  SizedBox(width: 30,),
                  Expanded(child:TextFormField(
                    controller: _kucun,
                    decoration: InputDecoration(
                      hintText: "请输入库存数量",
                      hintStyle: TextStyle(fontSize: 14)
                    ),
                  ))
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red)),Text("商品简介"),SizedBox(width: 5,)],) ,
                  SizedBox(width: 30,),
                  Expanded(child:TextFormField(
                    controller: _miaoshu,
                    decoration: InputDecoration(
                      hintText: "例：咬一口，回味无穷",
                      hintStyle: TextStyle(fontSize: 14)
                    ),
                  ))
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              decoration: BoxDecoration(
                // color: Colors.red
              ),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red)),Text("详情图片"),SizedBox(width: 5,)],) ,
                  SizedBox(width: 30,),
                  Expanded(child: Wrap(
                    children: [
                      Wrap(
                          spacing: 5,
                          runSpacing: 5,
                          children:
                          _getDetailPicture()
                        // GestureDetector(child: CachedNetworkImage(imageUrl: "https://img2.baidu.com/it/u=3800037915,2368949556&fm=26&fmt=auto&gp=0.jpg",width: 50,),onLongPress: (){print("长按删除");},),
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
                        child:GestureDetector(child: Row(
                          children: [
                            Container(
                              width: 60,
                              height: 60,
                              decoration: BoxDecoration(
                                border: Border.all(color: Colors.black , width: 0) ,
                              ),
                              child: Center(child: Text("+"),),
                            ),
                          ],
                        ),onTap: () async {
                          var s = await ImagePicker.pickImage(source: ImageSource.gallery);
                          File file = new File(s.path);// 获取文件路径
                          var res = await HttpUtil().upload("/upload", file);
                          detailPicture.add("tmp/"+res['data']);
                          // detailPictures = json.encode(detailPicture);
                          setState(()  {

                          });
                        },),
                      ),
                    ],
                  ))
                ],
              ),
            ),
            // Container(
            //   padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
            //   decoration: BoxDecoration(
            //     // color: Colors.red
            //   ),
            //   child: Row(
            //     children: [
            //       Row(children: [Text("*",style: TextStyle(color: Colors.red)),Text("商品详情"),SizedBox(width: 5,)],) ,
            //       SizedBox(width: 30,),
            //       Expanded(child: Column(
            //         children: [
            //           // TextFormField(
            //           //   keyboardType: TextInputType.multiline,
            //           //   // maxLines:2,//限制两行
            //           //   maxLines:null,
            //           //   inputFormatters: [LengthLimitingTextInputFormatter(50)],
            //           //   decoration: InputDecoration(
            //           //     border: InputBorder.none,
            //           //     hintText: "限50字",
            //           //   ),
            //           // ),
            //           Container(
            //             padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
            //             child: Row(
            //               mainAxisAlignment: MainAxisAlignment.spaceAround,
            //               children: [
            //                 GestureDetector(child: Row(
            //                   children: [
            //                     tu6==null?Text(""): _ImageView(_imgPath6,tu6),
            //                     tu6!=null?Text(""): Container(
            //                       width: 60,
            //                       height: 60,
            //                       decoration: BoxDecoration(
            //                         border: Border.all(color: Colors.black , width: 0) ,
            //                       ),
            //                       child: Center(child: Text("+"),),
            //                     ),
            //                   ],
            //                 ),onTap: () async {
            //                   final ImagePicker _picker = ImagePicker();
            //                   var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //                   _imgPath6=s;
            //                   File file = new File(s.path);// 获取文件路径
            //                   var res = await HttpUtil().upload("/upload", file);
            //                   tu6 = "tmp/"+res['data'];
            //                   setState(()  {
            //
            //                   });
            //                 },),
            //                 GestureDetector(child: Row(
            //                   children: [
            //                     tu7==null?Text(""): _ImageView(_imgPath7,tu7),
            //                     tu7!=null?Text(""): Container(
            //                       width: 60,
            //                       height: 60,
            //                       decoration: BoxDecoration(
            //                         border: Border.all(color: Colors.black , width: 0) ,
            //                       ),
            //                       child: Center(child: Text("+"),),
            //                     ),
            //                   ],
            //                 ),onTap: () async {
            //                   final ImagePicker _picker = ImagePicker();
            //                   var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //                   _imgPath7=s;
            //                   File file = new File(s.path);// 获取文件路径
            //                   var res = await HttpUtil().upload("/upload", file);
            //                   tu7 = "tmp/"+res['data'];
            //                   setState(()  {
            //
            //                   });
            //                 },),
            //                 GestureDetector(child: Row(
            //                   children: [
            //                     tu8==null?Text(""): _ImageView(_imgPath8,tu8),
            //                     tu8!=null?Text(""): Container(
            //                       width: 60,
            //                       height: 60,
            //                       decoration: BoxDecoration(
            //                         border: Border.all(color: Colors.black , width: 0) ,
            //                       ),
            //                       child: Center(child: Text("+"),),
            //                     ),
            //                   ],
            //                 ),onTap: () async {
            //                   final ImagePicker _picker = ImagePicker();
            //                   var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //                   _imgPath8=s;
            //                   File file = new File(s.path);// 获取文件路径
            //                   var res = await HttpUtil().upload("/upload", file);
            //                   tu8 = "tmp/"+res['data'];
            //                   setState(()  {
            //
            //                   });
            //                 },),
            //               ],
            //             )
            //           ),
            //         ],
            //       ))
            //     ],
            //   ),
            // ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("是否多规格"),
                  SizedBox(width: 30,),
                  isMoreGuige==false || guiges.length<1 ? Switch(  // 显示开关或者"查看"
                    value: isMoreGuige,
                    activeColor: Colors.blue,
                    onChanged: (value) {
                      setState(() async {
                        isMoreGuige=value;
                        if(value==true){
                          detailPictures = json.encode(detailPicture);
                          fuwuPictures = json.encode(fuwuPicture);
                          gName = _gName.text.trim();gPrice = _gPrice.text.trim();danwei = _danwei.text.trim();kucun = _kucun.text.trim();miaoshu = _miaoshu.text.trim();
                          List value = await MyRoute.push(GoodsGuige(gName: gName,gPrice: gPrice,danwei: danwei,kucun: kucun,miaoshu: miaoshu,goodsTypeName: goodsTypeName,tu1: tu1,tu2: tu2,tu3: tu3,tu4: tu4,tu5: tu5,guiges: guiges,tu6: tu6,tu7: tu7,tu8: tu8,type: type,id: id,fuwuPicture: fuwuPictures,detailPicture: detailPictures,));
                          if(value!=null){
                            guiges=value;
                          }
                        }
                      });
                    },
                  ):GestureDetector(child: Text("查看"),onTap: () async {
                    detailPictures = json.encode(detailPicture);
                    fuwuPictures = json.encode(fuwuPicture);
                    gName = _gName.text.trim();gPrice = _gPrice.text.trim();danwei = _danwei.text.trim();kucun = _kucun.text.trim();miaoshu = _miaoshu.text.trim();
                    List value = await MyRoute.push(GoodsGuige(gName: gName,gPrice: gPrice,danwei: danwei,kucun: kucun,miaoshu: miaoshu,goodsTypeName: goodsTypeName,tu1: tu1,tu2: tu2,tu3: tu3,tu4: tu4,tu5: tu5,guiges: guiges,tu6: tu6,tu7: tu7,tu8: tu8,type: type,id: id,fuwuPicture: fuwuPictures,detailPicture: detailPictures,));
                    if(value!=null){
                      guiges=value;
                      setState(() {

                      });
                    }
                  },),
                ],
              ),
            ),
            // Row(
            //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //   children: [
            //     Text("是否支持外卖"),
            //     SizedBox(width: 30,),
            //     Switch(
            //       value: false,
            //       activeColor: Colors.blue,
            //       onChanged: (value) {
            //         setState(() {
            //         });
            //       },
            //     ),
            //   ],
            // ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
              decoration: BoxDecoration(
                // color: Colors.red
              ),
              child: Row(
                children: [
                  Row(children: [Text("*",style: TextStyle(color: Colors.red)),Text("商品图片"),SizedBox(width: 5,)],) ,
                  SizedBox(width: 30,),
                  Expanded(child: Wrap(
                    children: [
                      Wrap(
                          spacing: 5,
                          runSpacing: 5,
                          children:
                          _getFuwuPicture()
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
                        child:GestureDetector(child: Row(
                          children: [
                            Container(
                              width: 60,
                              height: 60,
                              decoration: BoxDecoration(
                                border: Border.all(color: Colors.black , width: 0) ,
                              ),
                              child: Center(child: Text("+"),),
                            ),
                          ],
                        ),onTap: () async {
                          var s = await ImagePicker.pickImage(source: ImageSource.gallery);
                          File file = new File(s.path);// 获取文件路径
                          var res = await HttpUtil().upload("/upload", file);
                          fuwuPicture.add("tmp/"+res['data']);
                          // fuwuPictures = json.encode(fuwuPicture);
                          setState(()  {

                          });
                        },),
                      ),
                    ],
                  ))
                ],
              ),
            ),
            // Container(
            //   padding: EdgeInsets.fromLTRB(10, 10, 0, 10),
            //   decoration: BoxDecoration(
            //     // color: Colors.red,
            //   ),
            //   child: Column(
            //     children: [
            //       Row(children: [
            //         Container(
            //           child:  Row(
            //             children: [Text("*",style: TextStyle(color: Colors.red)),Text("商品图片")],
            //           ),
            //           margin: EdgeInsets.symmetric(vertical: 5),
            //         )
            //       ],),
            //       Row(
            //         mainAxisAlignment: MainAxisAlignment.spaceAround,
            //         children: [
            //           GestureDetector(child: Row(
            //             children: [
            //               tu1==null?Text(""): _ImageView(_imgPath1,tu1),
            //               tu1!=null?Text(""): Container(
            //                 width: 60,
            //                 height: 60,
            //                 decoration: BoxDecoration(
            //                   border: Border.all(color: Colors.black , width: 0) ,
            //                 ),
            //                 child: Center(child: Text("+"),),
            //               ),
            //             ],
            //           ),onTap: () async {
            //             final ImagePicker _picker = ImagePicker();
            //             var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //             _imgPath1=s;
            //             File file = new File(s.path);// 获取文件路径
            //             var res = await HttpUtil().upload("/upload", file);
            //             tu1 = "tmp/"+res['data'];
            //             setState(()  {
            //
            //             });
            //           },),
            //           GestureDetector(child: Row(
            //             children: [
            //               tu2==null?Text(""): _ImageView(_imgPath2,tu2),
            //               tu2!=null?Text(""): Container(
            //                 width: 60,
            //                 height: 60,
            //                 decoration: BoxDecoration(
            //                   border: Border.all(color: Colors.black , width: 0) ,
            //                 ),
            //                 child: Center(child: Text("+"),),
            //               ),
            //             ],
            //           ),onTap: () async {
            //             final ImagePicker _picker = ImagePicker();
            //             var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //             _imgPath2=s;
            //             File file = new File(s.path);// 获取文件路径
            //             var res = await HttpUtil().upload("/upload", file);
            //             tu2 = "tmp/"+res['data'];
            //             setState(()  {
            //
            //             });
            //           },),
            //           GestureDetector(child: Row(
            //             children: [
            //               tu3==null?Text(""): _ImageView(_imgPath3,tu3),
            //               tu3!=null?Text(""): Container(
            //                 width: 60,
            //                 height: 60,
            //                 decoration: BoxDecoration(
            //                   border: Border.all(color: Colors.black , width: 0) ,
            //                 ),
            //                 child: Center(child: Text("+"),),
            //               ),
            //             ],
            //           ),onTap: () async {
            //             final ImagePicker _picker = ImagePicker();
            //             var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //             _imgPath3=s;
            //             File file = new File(s.path);// 获取文件路径
            //             var res = await HttpUtil().upload("/upload", file);
            //             tu3 = "tmp/"+res['data'];
            //             setState(()  {
            //
            //             });
            //           },),
            //           GestureDetector(child: Row(
            //             children: [
            //               tu4==null?Text(""): _ImageView(_imgPath4,tu4),
            //               tu4!=null?Text(""): Container(
            //                 width: 60,
            //                 height: 60,
            //                 decoration: BoxDecoration(
            //                   border: Border.all(color: Colors.black , width: 0) ,
            //                 ),
            //                 child: Center(child: Text("+"),),
            //               ),
            //             ],
            //           ),onTap: () async {
            //             final ImagePicker _picker = ImagePicker();
            //             var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //             _imgPath4=s;
            //             File file = new File(s.path);// 获取文件路径
            //             var res = await HttpUtil().upload("/upload", file);
            //             tu4 = "tmp/"+res['data'];
            //             setState(()  {
            //
            //             });
            //           },),
            //           GestureDetector(child: Row(
            //             children: [
            //               tu5==null?Text(""): _ImageView(_imgPath5,tu5),
            //               tu5!=null?Text(""): Container(
            //                 width: 60,
            //                 height: 60,
            //                 decoration: BoxDecoration(
            //                   border: Border.all(color: Colors.black , width: 0) ,
            //                 ),
            //                 child: Center(child: Text("+"),),
            //               ),
            //             ],
            //           ),onTap: () async {
            //             final ImagePicker _picker = ImagePicker();
            //             var s = await ImagePicker.pickImage(source: ImageSource.gallery);
            //             _imgPath5=s;
            //             File file = new File(s.path);// 获取文件路径
            //             var res = await HttpUtil().upload("/upload", file);
            //             tu5 = "tmp/"+res['data'];
            //             setState(()  {
            //
            //             });
            //           },),
            //         ],
            //       )
            //     ],
            //   ),
            // ),
            SizedBox(height: 10,),
            Container(
              padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Row(
                children: [
                  Expanded(child: GestureDetector(
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Center(child: Text("保存"),),
                      color: Colors.red,
                    ),
                    onTap: () async {
                      // print("点击保存了");
                      gName = _gName.text.trim();gPrice = _gPrice.text.trim();danwei = _danwei.text.trim();kucun = _kucun.text.trim();miaoshu = _miaoshu.text.trim();
                      // print("商品名称："+gName+" 价格："+gPrice+" 单位："+danwei+" 库存："+kucun+" 描述："+miaoshu);
                      if(gName.length<1){Fluttertoast.showToast(msg: "商品名称不能为空");return ;}
                      final reg = RegExp(r'^-?[0-9]+');// 验证是否为数字
                      final zhengshu = RegExp(r'^[+]{0,1}(\d+)$');// 验证是否为正整数
                      if(!reg.hasMatch(gPrice)){Fluttertoast.showToast(msg: "商品价格只能是数字");return ;}
                      if(double.parse(gPrice) < 0){Fluttertoast.showToast(msg: "商品价格需大于或等于0");return ;}
                      if(danwei.length<1){Fluttertoast.showToast(msg: "商品单位不能为空");return ;}
                      if(!zhengshu.hasMatch(kucun)){Fluttertoast.showToast(msg: "库存只能为正整数");return ;}
                      if(miaoshu.length<1){Fluttertoast.showToast(msg: "商品简介不能为空");return ;}
                      if(detailPicture.length<1){Fluttertoast.showToast(msg: "请上传详情图片");return ;}
                      if(fuwuPicture.length<1){Fluttertoast.showToast(msg: "请上传商品图片");return ;}
                      // if(tu6==null || tu6==""){Fluttertoast.showToast(msg: "请上传商品详情图片");return ;}
                      // if(tu1==null || tu1==""){Fluttertoast.showToast(msg: "请上传商品图片");return ;}

                      // tu1 != null ? tu1=tu1.toString().substring(tu1.toString().lastIndexOf("/")+1,tu1.toString().length) : tu1=tu1;
                      // tu2 != null ? tu2=tu2.toString().substring(tu2.toString().lastIndexOf("/")+1,tu2.toString().length) : tu2=tu2;
                      // tu3 != null ? tu3=tu3.toString().substring(tu3.toString().lastIndexOf("/")+1,tu3.toString().length) : tu3=tu3;
                      // tu4 != null ? tu4=tu4.toString().substring(tu4.toString().lastIndexOf("/")+1,tu4.toString().length) : tu4=tu4;
                      // tu5 != null ? tu5=tu5.toString().substring(tu5.toString().lastIndexOf("/")+1,tu5.toString().length) : tu5=tu5;
                      // tu6 != null ? tu6=tu6.toString().substring(tu6.toString().lastIndexOf("/")+1,tu6.toString().length) : tu6=tu6;
                      // tu7 != null ? tu7=tu7.toString().substring(tu7.toString().lastIndexOf("/")+1,tu7.toString().length) : tu7=tu7;
                      // tu8 != null ? tu8=tu8.toString().substring(tu8.toString().lastIndexOf("/")+1,tu8.toString().length) : tu8=tu8;
                      // print("商品名称："+gName+" 商品类型："+goodsTypeName+" 商品价格："+gPrice+" 单位："+danwei+" 库存："+kucun+" 商品描述："+miaoshu);
                      // print("商品规格："+guiges.toString()+" 图片1："+tu1+" 图片6："+tu6);
                      var map = new HashMap<String,dynamic>();
                      String guige = json.encode(guiges);
                      map['gname']=gName;map['gprice']=gPrice;map['gtypeId']=goodsTypeName;map['gdanwei']=danwei;map['kucun']=kucun;map['jianjie']=miaoshu;map['guige']=guige;map['mid']=merchantID;
                      // map['gimages']=tu1;map['image2']=tu2;map['image3']=tu3;map['image4']=tu4;map['image5']=tu5;map['image6']=tu6;map['image7']=tu7;map['image8']=tu8;
                      map['gimages']="11111";map['image6']="66666";
                      map['fuwuPicture'] = json.encode(fuwuPicture);map['detailPicture']=json.encode(detailPicture);
                      if(type==1){//新增商品
                        // Response response = await HttpUtil().post("waimai/mGoods/addMGoods",map);
                        Response response = await HttpUtil().post("waimai/mGoods/addFuwuMGoods",map);
                        var x = json.decode(response.toString());
                        if(x['code']==200){
                          Fluttertoast.showToast(
                              msg: "新增成功"
                          );
                          MyRoute.push(Index(showIndex: "1",mType: mType,));
                        }else{
                          Fluttertoast.showToast(
                              msg: x['msg']
                          );
                        }
                      }else{//修改商品
                        map['id']=id;
                        // Response response = await HttpUtil().post("waimai/mGoods/updateMGoods",map);
                        Response response = await HttpUtil().post("waimai/mGoods/updateFuwuMGoods",map);
                        var x = json.decode(response.toString());
                        if(x['code']==200){
                          Fluttertoast.showToast(
                              msg: "修改成功"
                          );
                          MyRoute.push(Index(showIndex: "1",mType: mType,));
                        }else{
                          Fluttertoast.showToast(
                              msg: x['msg']
                          );
                        }
                      }
                    },
                  ))
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /*图片控件*/
  Widget _ImageView(imgPath,tu) {
    // if (imgPath == null) {
    //   return Center(
    //     child: Text("请选择图片或拍照"),
    //   );
    // } else {
    if (tu == null) {
      return Center(
        child: Text("请选择图片或拍照"),
      );
    } else {
      // return type!=2 ? Image.network(Config.getInstance().getHost()+'tmp/'+tu,width: ScreenUtil().setWidth(90),height: ScreenUtil().setHeight(100),) : Image.network(Config.getInstance().getHost()+tu,width: ScreenUtil().setWidth(90),height: ScreenUtil().setHeight(100),);
      return Image.network(Config.getInstance().getHost()+tu,width: ScreenUtil().setWidth(90),height: ScreenUtil().setHeight(100),);
    }
  }

  //循环详情图片
  _getDetailPicture(){
    List<Widget> x = [];
    for(int i=0;i<detailPicture.length;i++){
      x.add(GestureDetector(child: CachedNetworkImage(imageUrl: Config.getInstance().getHost()+detailPicture[i],width: 50,),onLongPress: (){
        detailPicture.removeAt(i);
        // detailPictures = json.encode(detailPicture);
        setState(() {

        });
      },)
      );
    }
    return  x;
  }

  //循环服务图片
  _getFuwuPicture(){
    List<Widget> x = [];
    for(int i=0;i<fuwuPicture.length;i++){
      x.add(GestureDetector(child: CachedNetworkImage(imageUrl: Config.getInstance().getHost()+fuwuPicture[i],width: 50,),onLongPress: (){
        // print("长按删除");print("下标："+i.toString());
        fuwuPicture.removeAt(i);
        // fuwuPictures = json.encode(fuwuPicture);
        setState(() {

        });
      },)
      );
    }
    return  x;
  }

}
